<?php

namespace Apipad\Controller;

class HomeController extends ComController
{
    //收款码
    public function payment_code()
    {
        $uid = UID;
        $model_user = M("user");
        $where['issh'] = 1;
        $where['uid'] = $uid;
        $rs_systemInfo = $model_user->where($where)->field('IFNULL(zhifubao_pay,"") as zhifubao_pay,IFNULL(weixin_pay,"") as weixin_pay')->find();
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['data'] = $rs_systemInfo;
        exit(json_encode($data));
    }

    //左侧分类
    public function mysqls_cate()
    {
        $model = D('UserMysqlCate');
        $map = [];
        $map['uid'] = UID;
        $map['pid'] = 0;
        $map['stype'] = 0;
        $map['isdel'] = 0;
        $list = $model->where($map)->order('add_time desc')->select();
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['data']['list'] = $list;
        exit(json_encode($data));
    }

    //商品列表  --库存数量大于0的显示
    public function goods_list()
    {
        $detail = I('');
        $cate_id = $detail['cate_id'];
        if (empty($cate_id)) {
            $data['code'] = 0;
            $data['msg'] = '分类不能为空';
            exit(json_encode($data));
        }
        //类型判断是否为当前客户
        $cat_model = D('UserMysqlCate');
        $cat_info = $cat_model->where(['id' => $cate_id, 'stype' => 0, 'uid' => UID, 'isdel' => 0])->find();
        if (empty($cat_info)) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '当前类型不属于当前客户';
            exit(json_encode($data_cc));
        }
        $page = $detail['page'] ? $detail['page'] : 1;
        $limit = $detail['limit'] ? $detail['limit'] : 20;
        $model = D('UserGoods');
        $cate_model = D('UserMysqlCate');
        $cate_lists = $cate_model->getChildIds($cate_id);
        $cate_lists[] = trim($cate_id);
        $cate_str = implode(',', $cate_lists);
        $map = [];
        if (I('keyword')) {
            $map['goods_name|goods_sn|goods_attr'] = array('like', '%' . I('keyword') . '%');
        }
        $map['_string'] = "FIND_IN_SET(cate_id,'" . $cate_str . "')";
        $map['uid'] = UID;
        $map['isdel'] = 0;
        $count = $model->where($map)->count();
        $list = $model->where($map)->order('add_time desc')->page($page, $limit)->select();
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['data']['count'] = $count;
        $data['data']['list'] = $list;
        exit(json_encode($data));
    }

    //商品详情---库存大于0的
    public function goods_detail()
    {
        $detail = I('');
        $goods_id = $detail['goods_id'];
        if (empty($goods_id)) {
            $data['code'] = 0;
            $data['msg'] = '商品ID不能为空';
            exit(json_encode($data));
        }
        $page = $detail['page'] ? $detail['page'] : 1;
        $limit = $detail['limit'] ? $detail['limit'] : 20;
        $g_model = D('UserGoods');
        $ga_model = D('UserGoodsAttr');
        $g_info = $g_model->where(['id' => $goods_id, 'uid' => UID, 'isdel' => 0])->find();
        if (empty($g_info)) {
            $data['code'] = 0;
            $data['msg'] = '商品信息不存在';
            exit(json_encode($data));
        }
        $map = [];
        if (I('keyword')) {
            $map['color|guige_name'] = array('like', '%' . I('keyword') . '%');
        }
        $map['goods_id'] = $goods_id;
        $map['isdel'] = 0;
        $map['uid'] = UID;
        $count = $ga_model->where($map)->count();
        $list = $ga_model->where($map)->order('add_time desc')->page($page, $limit)->select();
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['data']['count'] = $count;
        $data['data']['list'] = $list;
        exit(json_encode($data));
    }

    //查询大类和子分类
    function get_child($cate_id, $type)
    {
        $user_mysql_cate = M("user_mysql_cate");
        $uid = UID;
        $list = $user_mysql_cate->where(['pid' => $cate_id, 'uid' => $uid, 'stype' => $type])->select();
        $mcate = $cate_id . ",";
        if ($list) {
            foreach ($list as $k => $val) {
                $mcate .= $val['id'] . ",";
            }
        }
        $mcate = rtrim($mcate, ',');
        return $mcate;
    }

    //添加购物车
    public function add_cart()
    {
        $detail = I('');
        $discount = $detail['discount'];
        $goods_id = $detail['goods_id'];
        $goods_attr_id = $detail['goods_attr_id'];
        $order_num = $detail['order_num'];
        if (empty($goods_id)) {
            $data['code'] = 0;
            $data['msg'] = '商品ID不能为空';
            exit(json_encode($data));
        }
        $g_model = D('UserGoods');
        $ga_model = D('UserGoodsAttr');
        $oc_model = M('user_order_cart');
        $g_info = $g_model->where(['id' => $goods_id, 'uid' => UID, 'isdel' => 0])->find();
        if (empty($g_info)) {
            $data['code'] = 0;
            $data['msg'] = '商品信息不存在';
            exit(json_encode($data));
        }
        $s_dat = [];
        $oc_model->startTrans();
        foreach ($goods_attr_id as $k => $v) {
            if (empty($v)) {
                $data['code'] = 0;
                $data['msg'] = '商品详情ID不能为空';
                exit(json_encode($data));
            }
            $ga_info = $ga_model->where(['id' => $v, 'uid' => UID, 'isdel' => 0])->find();
            if (empty($ga_info)) {
                $data['code'] = 0;
                $data['msg'] = '商品详情信息不存在';
                exit(json_encode($data));
            }
            $s_data = [];
            if ($order_num[$k] > 0) {
                $s_data['uid'] = UID;
                $s_data['goods_id'] = $goods_id;
                $s_data['goods_attr_id'] = $v;
                $s_data['number'] = $order_num[$k];
                $s_data['discount'] = $discount;
                $s_data['price'] = $ga_info['price'];
                $s_data['goods_name'] = $g_info['goods_name'];
                $s_data['goods_sn'] = $g_info['goods_sn'];
                $s_data['goods_attr'] = $g_info['goods_attr'];
                $s_data['unit'] = $g_info['unit'];
                $s_data['guige'] = $ga_info['guige_name'];
                $s_data['color'] = $ga_info['color'];
                $s_data['add_time'] = time();
                $s_dat[] = $s_data;
            }
        }
        $result = $oc_model->addAll($s_dat);
        if ($result) {
            $oc_model->commit();
            $data['code'] = 200;
            $data['msg'] = '加入购物车成功';
            exit(json_encode($data));
        } else {
            $data['code'] = 0;
            $data['msg'] = '加入购物车失败';
            exit(json_encode($data));
        }
    }

    //已选款式
    public function cart_index()
    {
        $oc_model = D('UserOrderCart');
        $gd_model = D("UserGoodsAttr");
        $where['uid'] = UID;
        $where['isdel'] = 0;
        $list = $oc_model->where($where)->select();
        $total_amount = 0;
        $total_number = 0;
        foreach ($list as $k => $v) {
            $total_amount = bcadd($total_amount, $v['amount'], 2);
            $total_number = bcadd($total_number, $v['number'], 2);
            $gd_info = $gd_model->where(['id' => $v['goods_attr_id'], 'goods_id' => $v['goods_id'], 'uid' => UID, 'isdel' => 0])->find();
            $list[$k]['stock'] = $gd_info['stock'];
            $list[$k]['discount'] = floatval($v['discount']);
        }
        $data['code'] = 200;
        $data['msg'] = '获取成功';
        $data['data']['total_amount'] = floatval($total_amount);
        $data['data']['total_number'] = floatval($total_number);
        $data['data']['list'] = $list;
        exit(json_encode($data));
    }

    //购物车清空
    public function cart_clear()
    {
        $oc_model = D('UserOrderCart');
        $where['uid'] = UID;
        $where['isdel'] = 0;
        $result = $oc_model->where($where)->save(['isdel' => 1, 'del_time' => time()]);
        if ($result) {
            $data['code'] = 200;
            $data['msg'] = '清空购物车成功';
            exit(json_encode($data));
        } else {
            $data['code'] = 0;
            $data['msg'] = '清空购物车失败';
            exit(json_encode($data));
        }
    }

    //购物车单条删除
    public function cart_del()
    {
        $detail = I('');
        $cart_id = $detail['cart_id'];
        if (empty($cart_id)) {
            $data['code'] = 0;
            $data['msg'] = '购物车数据Id不能为空';
            exit(json_encode($data));
        }
        $oc_model = D('UserOrderCart');
        $where['uid'] = UID;
        $where['isdel'] = 0;
        $where['id'] = $cart_id;
        $info = $oc_model->where($where)->find();
        if (empty($info)) {
            $data['code'] = 0;
            $data['msg'] = '购物车数据不存在';
            exit(json_encode($data));
        }
        $result = $oc_model->where($where)->save(['isdel' => 1, 'del_time' => time()]);
        if ($result) {
            $data['code'] = 200;
            $data['msg'] = '删除成功';
            exit(json_encode($data));
        } else {
            $data['code'] = 0;
            $data['msg'] = '删除失败';
            exit(json_encode($data));
        }
    }


    //编辑
    public function cart_edit()
    {
        $detail = I('');
        $oc_model = D('UserOrderCart');
        $oc_model->startTrans();
        $cart_id = $detail['cart_id'];
        $order_num = $detail['order_num'];
        $discount = $detail['discount'];
        foreach ($cart_id as $k => $v) {
            if (empty($v)) {
                $data['code'] = 0;
                $data['msg'] = '购物车数据ID不能为空';
                exit(json_encode($data));
            }
            if (empty($order_num[$k])) {
                $data['code'] = 0;
                $data['msg'] = '请输入下单数量';
                exit(json_encode($data));
            }
            $res = $oc_model->where(['id' => $v, 'uid' => UID, 'isdel' => 0])->save(['number' => $order_num[$k], 'discount' => $discount[$k]]);
        }
        $oc_model->commit();
        $data['code'] = 200;
        $data['msg'] = '编辑成功';
        exit(json_encode($data));

    }

    //提交订单
    public function order_add()
    {
        $detail = I('post.', '', 'strip_tags');
        $customer_name = $detail['customer_name'];
        $mobile = $detail['mobile'];
        $remark = $detail['remark'];
        if (empty($customer_name)) {
            $data['code'] = 0;
            $data['msg'] = '请填写客户姓名';
            exit(json_encode($data));
        }
        if (empty($mobile)) {
            $data['code'] = 0;
            $data['msg'] = '请填写联系方式';
            exit(json_encode($data));
        }
        $uid = UID;
        $oc_model = D('UserOrderCart');
        $where['uid'] = $uid;
        $where['isdel'] = 0;
        $list = $oc_model->where($where)->select();
        if (empty($list)) {
            $data['code'] = 0;
            $data['msg'] = '购物车为空';
            exit(json_encode($data));
        }
        $model_user_order = M("user_order");
        $model_user_detail = M('user_order_detail');
        $model_user_order->startTrans();
        //查询订单数
        $countc = $model_user_order->where(['uid' => $uid, 'isdel' => 0])->count();
        $cocda = $countc + 1;
        $s_data['ordersn'] = 'MDK8000' . $uid . $cocda;
        $s_data['goods_number'] = 0;
        $s_data['uid'] = $uid;
        $s_data['stype'] = 1;
        $s_data['customer_name'] = $customer_name;
        $s_data['mobile'] = $mobile;
        $s_data['remark'] = $remark;
        $s_data['address'] = $detail['address'];
        $s_data['region'] = $detail['region'];
        $s_data['dqvalue'] = json_encode($detail['dqvalue']);
        $s_data['add_time'] = time();
        $res = $model_user_order->add($s_data);
        if ($res) {
            $sd_dat = [];
            $amount = 0;
            $disamount = 0;
            $order_number = 0;
            $sd_data['uid'] = $uid;
            $sd_data['order_id'] = $res;
            foreach ($list as $k => $v) {
                $sd_data['goods_id'] = $v['goods_id'];
                $sd_data['goods_attr_id'] = $v['goods_attr_id'];
                $sd_data['number'] = $v['number'];
                $sd_data['discount'] = $v['discount'];
                $sd_data['price'] = $v['price'];
                $sd_data['goods_name'] = $v['goods_name'];
                $sd_data['goods_sn'] = $v['goods_sn'];
                $sd_data['goods_attr'] = $v['goods_attr'];
                $sd_data['guige_name'] = $v['guige'];
                $sd_data['color'] = $v['color'];
                $sd_data['unit'] = $v['unit'];
                $sd_data['add_time'] = time();
                $sd_dat[] = $sd_data;
                $goods_amount = bcmul($v['price'], $v['number'], 2);
                if ($v['discount'] > 0) {
                    $dis_price = bcmul($v['price'], $v['discount'], 2);
                } else {
                    $dis_price = $v['price'];
                }
                $goods_disamount = bcmul($dis_price, $v['number'], 2);
                $amount = bcadd($amount, $goods_amount, 2);
                $disamount = bcadd($disamount, $goods_disamount, 2);
                $order_number = bcadd($order_number, $v['number']);
            }
            $od_result = $model_user_detail->addAll($sd_dat);
            if ($od_result) {
                $o_res = $model_user_order->where(['id' => $res, 'uid' => $uid, 'isdel' => 0])->save(['amount' => $amount, 'dis_amount' => $disamount, 'goods_number' => $order_number]);
                if ($o_res) {
                    //清空购物车
                    $oc_model = D('UserOrderCart');
                    $oc_where['uid'] = UID;
                    $oc_where['isdel'] = 0;
                    $oc_result = $oc_model->where($where)->save(['isdel' => 1, 'del_time' => time()]);
                    if ($oc_result) {
                        $model_user_order->commit();
                        $data['code'] = 200;
                        $data['msg'] = '提交成功';
                        exit(json_encode($data));
                    } else {
                        $data['code'] = 0;
                        $data['msg'] = '提交失败';
                        exit(json_encode($data));
                    }
                } else {
                    $data['code'] = 0;
                    $data['msg'] = '提交失败';
                    exit(json_encode($data));
                }
            } else {
                $data['code'] = 0;
                $data['msg'] = '提交失败';
                exit(json_encode($data));
            }
        } else {
            $data['code'] = 0;
            $data['msg'] = '提交失败';
            exit(json_encode($data));
        }
    }


    public function index_gongyi()
    {
        $detail = I('post.');
        $keyword = $detail['keyword'];
        $limit = $detail['limit'] ?: 10;
        $page = $detail['page'] ?: 1;
        $status = $detail['status'] ?: 0;
        $model = M('Imgcate');
        $where['is_delete'] = 0;
        $where['status'] = $status;
        $where['pid'] = 0;
        if ($keyword) {
            $where['title'] = array('like', "%" . $keyword . "%");
        }
        $count = $model->where($where)->count();
        $list = $model->where($where)->field('id,title,pid')->order('id asc')->page($page, $limit)->select();
        if ($list) {
            foreach ($list as $k => $val) {
                $list[$k]['level'] = 1;
                $lis5c = $model->where(['pid' => $val['id'], 'is_delete' => 0])->field('id,title,pid')->order('id desc')->select();
                $list[$k]['counts'] = count($lis5c);
                $list[$k]['child_list'] = $lis5c;
                if ($lis5c) {
                    foreach ($lis5c as $k1 => $v1) {
                        $list[$k]['child_list'][$k1]['level'] = 2;
                        $list[$k]['child_list'][$k1]['counts'] = count($this->get_imgcate_child($v1['id']));
                        $list[$k]['child_list'][$k1]['children'] = $this->get_imgcate_child($v1['id']);
                    }
                }
            }
        }
        $data_cc['code'] = 200;
        $data_cc['msg'] = "查询成功";
        $data_cc['data']['count'] = $count;
        $data_cc['data']['list'] = $list;
        exit(json_encode($data_cc));
    }

    //查询子分类
    function get_imgcate_child($pid)
    {
        $model = M('Imgcate');
        $lis5c = $model->where(['pid' => $pid, 'is_delete' => 0])->field('id,title,pid')->order('id desc')->select();
        return $lis5c;
    }

    //标准数据库默认分类
    public function default_mysqls_cate()
    {
        $model = D('UserMysqlDefaultCate');
        $map = [];
        $map['pid'] = 0;
        $map['stype'] = 1;
        $map['isdel'] = 0;
        $list = $model->where($map)->order('add_time desc')->select();
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['data']['list'] = $list;
        exit(json_encode($data));
    }
}
